package com.wugenqiang.test.Client.builder;

import com.google.gson.annotations.SerializedName;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

/**
 * @author wugenqiang
 * @Title:  Metric
 * @ProjectName kafka_flink_opentsdb
 * @Description:  Metric
 * @date 2018/8/29 10:29
 */
public class Metric {

    @SerializedName("metric")
    private String name; 		//metric指标名

    private long timestamp;		//时间戳

    private Double value;		//值

    private Map<String, String> tags = new HashMap<String, String>();//标记

    protected Metric(String name) {
        checkNotNull(name);
        this.name = name;
    }

    /**
     * Adds a tag to the data point.
     *
     * @param name
     *            tag identifier
     * @param value
     *            tag value
     * @return the metric the tag was added to
     */
    public Metric addTag(String name, String value) {

        checkNotNull(name);
        checkNotNull(value);
        tags.put(name, value);

        return this;
    }

    /**
     * Adds tags to the data point.
     *
     * @param tags
     *            map of tags
     * @return the metric the tags were added to
     */
    public Metric addTags(Map<String, String> tags) {
        checkNotNull(tags);
        this.tags.putAll(tags);

        return this;
    }

    /**
     * set the data point for the metric.
     *
     * @param timestamp
     *            when the measurement occurred
     * @param value
     *            the measurement value
     * @return the metric
     */
    protected Metric innerAddDataPoint(long timestamp, Double value) {
        checkArgument(timestamp > 0);
        checkNotNull(value);
        this.timestamp = timestamp;
        this.value = value;

        return this;
    }

    /**
     * Adds the data point to the metric with a timestamp of now.
     *
     * @param value
     *            the measurement value
     * @return the metric
     */
    public Metric setDataPoint(Double value) {
        return innerAddDataPoint(System.currentTimeMillis(), value);
    }

    public Metric setDataPoint(long timestamp, Double value) {
        return innerAddDataPoint(timestamp, value);
    }

    /**
     * Time when the data point was measured.
     *
     * @return time when the data point was measured
     */
    public long getTimestamp() {
        return timestamp;
    }

    public Double getValue() {
        return value;
    }

    /**
     * Returns the metric name.
     *
     * @return metric name
     */
    public String getName() {
        return name;
    }

    /**
     * Returns the tags associated with the data point.
     *
     * @return tag for the data point
     */
    public Map<String, String> getTags() {
        return Collections.unmodifiableMap(tags);
    }

}
